/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GuiaListaForm.java
 *
 * Created on 07/04/2010, 10:12:13 AM
 */

package agencia.form;

import agencia.Guia;
import agencia.controller.AgenciaController;
import agencia.controller.GuiaController;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author elvis
 */
public class GuiaListaForm extends javax.swing.JInternalFrame {

    private JDesktopPane jdesktopPane;
    private AgenciaController agenciaController;
    private GuiaController guiaController;

    public void setJdesktopPane(JDesktopPane jdesktopPane) {
        this.jdesktopPane = jdesktopPane;
    }
    public void setAgenciaController(AgenciaController agenciaController) {
        this.agenciaController = agenciaController;
    }
    public void setGuiaController(GuiaController guiaController) {
        this.guiaController = guiaController;
    }

    /** Creates new form GuiaListaForm */
    public GuiaListaForm() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblGuias = new javax.swing.JTable();
        jButton3 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setClosable(true);
        setTitle("Lista de Guias");

        tblGuias.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Codigo", "Nombre", "Ninio", "Adulto", "Adulto Mayor"
            }
        ));
        jScrollPane1.setViewportView(tblGuias);

        jButton3.setText("-");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton1.setText("+");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("E");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 394, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(68, Short.MAX_VALUE))
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 394, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 278, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(64, 64, 64)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 170, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        GuiaForm form = new  GuiaForm();
        form.setGuiaController(this.guiaController);
        form.setAgenciaController(this.agenciaController);
        form.setGuiaListaForm(this);
        form.setAccion(GuiaForm.ACCION_NUEVO);
        form.inicaForm();
        this.jdesktopPane.add(form);
        form.show();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
         if (tblGuias.getSelectedRow()!=-1){
           int cod = (Integer)tblGuias.getValueAt( tblGuias.getSelectedRow(), 0);
           Guia guia = guiaController.obtenerGuia(cod);
           GuiaForm form = new  GuiaForm();
            form.setGuiaController(this.guiaController);
            form.setAgenciaController(this.agenciaController);
            form.setGuia(guia);
            form.setAccion(GuiaForm.ACCION_MODIFICAR);
            form.setGuiaListaForm(this);
            form.inicaForm();
            this.jdesktopPane.add(form);
            form.show();

         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el guia a modificar");
         }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
         if (tblGuias.getSelectedRow()!=-1){
            int cod = (Integer)tblGuias.getValueAt( tblGuias.getSelectedRow(), 0);
            guiaController.eliminarGuia(cod);
            pintagrilla();
         }else{
          JOptionPane.showMessageDialog(rootPane, "Debe seleccionar el guia a eliminar");
         }
    }//GEN-LAST:event_jButton3ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblGuias;
    // End of variables declaration//GEN-END:variables

     public void pintagrilla(){
        DefaultTableModel tabla = (DefaultTableModel) tblGuias.getModel();
        tabla.setRowCount(0);
        List<Guia> paquetes = guiaController.getListaGuias();
        DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        for (Guia examen : paquetes) {
            Object[] fila = new Object[5];
            fila[0] = examen.getCodigo();
            fila[1] = examen.getNombre();
            fila[2] = examen.getSexo();
            fila[3] = df.format(examen.getNacimiento());
            fila[4] = examen.getAgencia().getNombre();
            tabla.addRow(fila);
        }
    }

     public void iniciaform(){
        pintagrilla();
     }

}
